﻿
using Microsoft.AspNetCore.SignalR;
using SignalR.Server.Hubs;

namespace SignalR.Server.BackgroundWorkers
{
    public class Worker : BackgroundService
    {
        private readonly ILogger<Worker> _logger;
        private readonly IHubContext<ChatHub> _chatHubContext;
        public Worker(ILogger<Worker> logger, IHubContext<ChatHub> chatHubContext)
        {
            _logger = logger;
            _chatHubContext = chatHubContext;
        }
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);

                await _chatHubContext.Clients.All.SendAsync("OnlineUsers", "System", $"当前在线Connection: {string.Join(',', ChatHub.ConnectedConnections.Keys)}");
                await Task.Delay(10000, stoppingToken);
            }
        }
    }
}
